package com.tbit.main.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 对象工具类
 *
 * @author Leon
 * 2019年8月21日 下午9:32:56
 */
@Slf4j
public class ObjectUtil {

    @SuppressWarnings("rawtypes")
    public static Object assign(Object soruce, Object target, String[] ignoreFields) {
        try {
            Class clsSource = soruce.getClass();
            Field[] fieldsSource = clsSource.getDeclaredFields();
            Class clsTarget = target.getClass();
            Field[] fieldsTarget = clsTarget.getDeclaredFields();
            List<String> igfields = new ArrayList<String>();
            if (ignoreFields != null && ignoreFields.length > 0) {
                igfields.addAll(Arrays.asList(ignoreFields));
            }
            for (int i = 0; i < fieldsSource.length; i++) {
                Field fS = fieldsSource[i];
                Field fT = fieldsTarget[i];
                fS.setAccessible(true);
                fT.setAccessible(true);
                if (fS.get(soruce) != null && !igfields.contains(fS.getName())) {
                    fT.set(target, fS.get(soruce));
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return target;
    }

    public static boolean statusParse(Integer n, Integer terStatus) {
        return ((1 << n) & terStatus) != 0;
    }

    /**
     * 根据属性名获取属性值
     */
    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            LogUtil.error(e.getMessage(), e);
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(statusParse(7, 16005));
    }

    /**
     * 获取属性名数组
     */
    private String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     */
    private List getFiledsInfo(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        List list = new ArrayList();
        Map infoMap = null;
        for (int i = 0; i < fields.length; i++) {
            infoMap = new HashMap();
            infoMap.put("type", fields[i].getType().toString());
            infoMap.put("name", fields[i].getName());
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     */
    public Object[] getFiledValues(Object o) {
        String[] fieldNames = this.getFiledName(o);
        Object[] value = new Object[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++) {
            value[i] = getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }
}
